<?php

namespace app\api\controller;

use app\api\validate\UserValidate;
use support\Request;
use think\facade\Db;
use Tinywan\Jwt\JwtToken;

class UserController extends BaseController
{
    /**
     * @title: 测试接口
     * @url: /api/user/index
     * @date: 2025/4/2
     * @time: 10:36
     * @param Request $request
     * @return \support\Response
     */
    public function index(Request $request)
    {
        return $this->success();
    }

    /**
     * 添加用户
     * @url: /api/user/add
     */
    public function add(Request $request)
    {
        $data = [
            'name'  => '',
            'age'  => 240,
            'email' => 'Tinywan@163.com'
        ];
        // 助手函数 推荐 验证错误会自动抛出异常
        validate($data, UserValidate::class);

        $validate = new UserValidate;
        if (!$validate->check($data)) {
            var_dump($validate->getError());
        }
        return $this->success('ok', ['error' => $validate->getError()]);
    }


    /**
     * 测试连接数据库
     * @url: /api/user/testDb
     */
    public function get(Request $request)
    {
        $user = Db::name('users')->where('id', '>', 1)->find();
        return $this->success('ok', ['user' => $user]);
    }




}
